﻿//using AppHelper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;


namespace BudgetSV.Common
{
    public static class PageClass
    {
        public static string XinxiTemp = "/UPFiles/2016信息采编/信息稿件模板.doc";
        public static string XinxiTempNew = "/UPFiles/2016信息采编/信息稿件模板new.doc";
        public static string NewsTemp = "/UPFiles/2016信息采编/每周动态模板.doc";
        public static string InfoTemp = "/UPFiles/2016信息采编/云南财政信息模板.doc";
        public static string KSNoticeTemp = "/UPFiles/2016信息采编/各处室信息稿采用情况模板.doc";
        public static string ZSNoticeTemp = "/UPFiles/2016信息采编/各州市信息稿采用情况模板.doc";
        public static string CreateDocPath = "/UPFiles/NewsDoc/";
        public static string templetPath = "/UPFiles/templateFile/";
        //限制插入文件的格式
        public static string[] fileTypes = { ".doc", ".docx" };
        #region 绑定下拉框
        /// <summary>
        /// 绑定下拉
        /// </summary>
        /// <param name="cb"></param>
        /// <param name="dataSource"></param>
        /// <param name="DataTextField"></param>
        /// <param name="DataValueField"></param>
        /// <param name="DataValueField">请选择</param>
        public static void BindComboBox(DropDownList cb, object dataSource, string DataTextField, string DataValueField, bool IsSelectAll)
        {
            cb.DataSource = null;
            cb.Items.Clear();
            cb.DataSource = dataSource;
            cb.DataValueField = DataValueField;
            cb.DataTextField = DataTextField;
            cb.DataBind();
            if (IsSelectAll)
            {
                ListItem allItem = new ListItem("--请选择--", "");
                cb.Items.Add(allItem);
                cb.SelectedIndex = cb.Items.Count - 1;
            }
            else
            {
                if (cb.Items.Count > 0)
                {
                    cb.SelectedIndex = 0;
                }
            }
        }

        public static void BindComboBox(DropDownList cb, object dataSource, string DataTextField, string DataValueField, string strTitle)
        {
            cb.DataSource = null;
            cb.Items.Clear();
            cb.DataSource = dataSource;
            cb.DataValueField = DataValueField;
            cb.DataTextField = DataTextField;
            cb.DataBind();

            cb.Items.Insert(0, strTitle);
            cb.SelectedIndex = 0;

            //if (IsSelectAll)
            //{
            //    ListItem allItem = new ListItem("--请选择--", "");
            //    cb.Items.Add(allItem);
            //    cb.SelectedIndex = cb.Items.Count - 1;
            //}
            //else
            //{
            //    if (cb.Items.Count > 0)
            //    {
            //        cb.SelectedIndex = 0;
            //    }
            //}
        }
        /// <summary>
        /// 吧一个枚举绑定到下拉框
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cb"></param>
        /// <param name="IsSelectAll"></param>
        public static void BindCombox<T>(DropDownList cb, bool IsSelectAll, bool IsComtainSelectAll = false)
        {
            cb.Items.Clear();
            cb.DataSource = ListTypeForEnum<T>(IsComtainSelectAll);
            cb.DataTextField = "text";
            cb.DataValueField = "value";
            cb.DataBind();
            if (IsSelectAll)
            {

                ListItem allItem = new ListItem();
                allItem.Text = "--请选择--";
                allItem.Value = "";
                cb.Items.Add(allItem);
                cb.SelectedIndex = cb.Items.Count - 1;
            }
            else
            {
                if (cb.Items.Count > 0)
                {
                    cb.SelectedIndex = 0;
                }
            }
        }
        public static IList ListTypeForEnum<T>(bool IsComtainSelectAll = false)
        {
            ArrayList list = new ArrayList();
            if (IsComtainSelectAll)
            {
                list.Add(new ListItem("--请选择--", ""));
            }
            foreach (int i in Enum.GetValues(typeof(T)))
            {
                ListItem listitem = new ListItem(getEnumName<T>(i.ToString()), i.ToString());
                list.Add(listitem);
            }
            return list;
        }
        #endregion
        /// <summary>
        /// 将字符串转换为Int
        /// </summary>
        /// <param name="t"></param>
        /// <returns>当转换失败时返回0</returns>
        public static int ToInt(this string t)
        {
            int iValue = 0;
            int.TryParse(t, out iValue);
            return iValue;
        }
        /// <summary>
        /// 将字符串转换为decimal
        /// </summary>
        /// <param name="t"></param>
        /// <returns>当转换失败时返回0</returns>
        public static decimal ToDecimal(this string t)
        {
            decimal iValue = 0;
            decimal.TryParse(t, out iValue);
            return iValue;
        }
        /// <summary>
        /// 将字符串转换为double
        /// </summary>
        /// <param name="t"></param>
        /// <returns>当转换失败时返回0</returns>
        public static double ToDouble(this string t)
        {
            double iValue = 0;
            double.TryParse(t, out iValue);
            return iValue;
        }
        /// <summary>
        /// 将字符串转换为DateTime
        /// </summary>
        /// <param name="t"></param>
        /// <returns>当转换失败时返回当前时间</returns>
        public static DateTime ToDateTime(this string t)
        {
            DateTime iValue = DateTime.Now;
            DateTime.TryParse(t, out iValue);
            return iValue;
        }

        public static string getEnumName<T>(string value)
        {
            string enumText = string.Empty;
            if (!string.IsNullOrEmpty(value))
            {
                //enumText = EnumText.GetText<T>(value);
            }
            return enumText;
        }

        /// <summary>
        /// 初始化对象中的字符串为空
        /// </summary>
        /// <param name="obj"></param>
        public static void InitString(object obj)
        {
            if (obj == null) return;

            var pis = obj.GetType().GetProperties();
            foreach (var p in pis)
            {
                if (p.PropertyType.Name == "String")
                {
                    p.SetValue(obj, "", null);
                }
            }
        }

        public static T GetValue<T>(string strText, T defaultValue)
        {
            if (string.IsNullOrWhiteSpace(strText)) return defaultValue;

            return (T)Convert.ChangeType(strText.Trim(), typeof(T));
        }

        public static decimal? GetDecimalValue(string strText)
        {
            if (string.IsNullOrWhiteSpace(strText)) return null;
            return Convert.ToDecimal(strText.Trim());
        }

        public static int? GetIntValue(string strText)
        {
            if (string.IsNullOrWhiteSpace(strText)) return null;
            return Convert.ToInt32(strText.Trim());
        }

        public static Nullable<T> GetValue2<T>(string strText) where T : struct
        {
            if (string.IsNullOrWhiteSpace(strText)) return null;
            return (T)Convert.ChangeType(strText.Trim(), typeof(T));
        }
        public static Nullable<T> GetEnum<T>(int iText) where T : struct
        {
           // return (T)Convert.ChangeType(iText, typeof(T));
            return (T)Enum.ToObject(typeof(T), iText);
        }

    }
}
